﻿using MaiPlanServer.IRepository;
using MaiPlanServer.IServices.IWarehouseServives;
using MaiPlanServer.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MaiPlanServer.Services.WarehouseIServives
{
    public class WarehouseSettingsManageServices : IWarehouseSettingsManageServices
    {
        public WarehouseSettingsManageServices(IBaseRepository BaseConn) {
            this.BaseConn = BaseConn;
        }

        public IBaseRepository BaseConn { get; }

        public async Task<bool> AddAsync(wm_warehouse model)
        {
            var result = await BaseConn.InsertAsync(model);
            return result;
        }

        public async Task<bool> DellAllAsync(string[] ruleId)
        {
            string sql = "delete wm_warehouse where Warehouse_id in @ruleId";
            var result = await BaseConn.DeleteAsync(sql, new { ruleId });
            return result;
        }

        public async Task<bool> DellAsync(string ruleId)
        {
            string sql = "delete wm_warehouse where Warehouse_id = @ruleId";
            var result = await BaseConn.DeleteAsync(sql, new { ruleId });
            return result;
        }

        public async Task<IEnumerable<wm_warehouse>> GetArmazemAsync(int PageIndex, int PageSize, string WarehouseName, string Warehousecode)
        {
            string sql = "select * from wm_warehouse where 1=1 and Warehouse_name like @WarehouseName and  Warehouse_code like @Warehousecode";

            sql += " order by Create_Time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only";

            var list = await BaseConn.QueryAsync<wm_warehouse>(sql,new {PageIndex,PageSize,WarehouseName ="%"+WarehouseName+"%",Warehousecode = "%" + Warehousecode + "%"});
            return list;
        }

        public async Task<int> GetCountAsync(string WarehouseName)
        {
            string sql = @" select count(1) from wm_warehouse where 1=1 and Warehouse_name like  @WarehouseName ";

            var conut = await BaseConn.GetRecoreCountAsync(sql, new { WarehouseName = "%" + WarehouseName + "%" });
            return conut;
        }

        public async Task<bool> UpdateAsync(wm_warehouse model)
        {
            var result = await BaseConn.UpdateAsync(model,"Warehouse_id");

            return result;
        }
    }
}
